/**
**********************************************************************
* @file     uart.h
* @brief    对文件的简要说明
* @details  对文件的详细说明
* @author   jcai
* @date     2022-05-18 20:24:08
* @copyright Copyright &copy; 2022 wy1107412334@163.com All rights reserved
* @version  v0.0.1
*
* \n Email:  wy1107412334@163.com
* \n Blog:   None
* \n Github: https://github.com/wy1107412334
* \n Gitee:  https://gitee.com/git.git1107412334.com
*
**********************************************************************
* @note
* None.
**********************************************************************
* @attention
* None.
**********************************************************************
@verbatim
@endverbatim
**********************************************************************
* @par 历史记录
* 1. v0.0.1: jcai, 2022-05-18 20:24:08
*   - 首次编写
* 2. v0.0.2: jcai, 2022-08-27 15:53:08
*   - 串口增加单字节数据发送函数
*
*<center>Copyright &copy; 2022 wy1107412334@163.com All rights reserved</center>
**********************************************************************
*/

#ifndef __UART_H__
#define __UART_H__


/* 头文件导入区 --------------------------------------------------- */
#include "public.h"


#ifdef __cplusplus
extern "C"
{
#endif

/** @addtogroup UART
* @brief uart modules
* @{
*/


/** @defgroup UART_CompilationDefines uart compilation defines
* @brief 条件编译定义
* @{
*/



/** defgroup UART_CompilationDefines
* @}
*/


/** @defgroup UART_Input_Interface uart Input API Interface
* @brief 模块输入API接口。即对外部少量依赖模块的API接口
* @{
*/



/** defgroup UART_Input_Interface
* @}
*/


/** @defgroup UART_Exported_MacroDefines uart exported macro defines
* @brief 全局宏定义
* @{
*/

/**
 * 标准库的标准I/O设备
 *
 * 标准库中 scanf(), printf() 的底层输入/输出设备.
 * 可用设备：UART1_DEV ~ UART3_DEV
 */
#define DEFAULT_STDOUT_DEV          UART2_DEV


//#define UART1_EN                    1
//#define UART1_IT_RX_EN              1
//#define UART1_IT_TX_EN              1
//#define UART1_IT_IDLE_EN            1
#define UART1_PIN_SELECT            1

#define UART1_DEV                   USART1
#define UART1_DEV_DATA_LENGTH       USART_WordLength_8b
#define UART1_DEV_PARITY            USART_Parity_No
#define UART1_DEV_STOPBITS          USART_StopBits_1
#define UART1_DEV_BAUD_RATE         115200

#define UART1_ISR_MAINPRIO          1
#define UART1_ISR_SUBPRIO           0

#if (UART1_PIN_SELECT == 1)
#define UART1_TX_GPIO_RCC           RCC_APB2Periph_GPIOA
#define UART1_TX_GPIO_PORT          GPIOA
#define UART1_TX_GPIO_PIN           GPIO_Pin_9
#define UART1_RX_GPIO_RCC           RCC_APB2Periph_GPIOA
#define UART1_RX_GPIO_PORT          GPIOA
#define UART1_RX_GPIO_PIN           GPIO_Pin_10
#define UART1_GPIO_PIN_REMAP        0
#elif (UART1_PIN_SELECT == 2)
#define UART1_TX_GPIO_RCC           RCC_APB2Periph_GPIOB
#define UART1_TX_GPIO_PORT          GPIOB
#define UART1_TX_GPIO_PIN           GPIO_Pin_6
#define UART1_RX_GPIO_RCC           RCC_APB2Periph_GPIOB
#define UART1_RX_GPIO_PORT          GPIOB
#define UART1_RX_GPIO_PIN           GPIO_Pin_7
#define UART1_GPIO_PIN_REMAP        1
#else
#error 宏 UART1_PIN_SELECT 的值只能为 1 或 2
#endif


#define UART2_EN                    1
//#define UART2_IT_RX_EN              1
//#define UART2_IT_TX_EN              1
//#define UART2_IT_IDLE_EN            1
#define UART2_PIN_SELECT            1

#define UART2_DEV                   USART2
#define UART2_DEV_DATA_LENGTH       USART_WordLength_8b
#define UART2_DEV_PARITY            USART_Parity_No
#define UART2_DEV_STOPBITS          USART_StopBits_1
#define UART2_DEV_BAUD_RATE         115200

#define UART2_ISR_MAINPRIO          1
#define UART2_ISR_SUBPRIO           1

#if (UART2_PIN_SELECT == 1)
#define UART2_TX_GPIO_RCC           RCC_APB2Periph_GPIOA
#define UART2_TX_GPIO_PORT          GPIOA
#define UART2_TX_GPIO_PIN           GPIO_Pin_2
#define UART2_RX_GPIO_RCC           RCC_APB2Periph_GPIOA
#define UART2_RX_GPIO_PORT          GPIOA
#define UART2_RX_GPIO_PIN           GPIO_Pin_3
#define UART2_GPIO_PIN_REMAP        0
#elif (UART2_PIN_SELECT == 2)
#define UART2_TX_GPIO_RCC           RCC_APB2Periph_GPIOD
#define UART2_TX_GPIO_PORT          GPIOD
#define UART2_TX_GPIO_PIN           GPIO_Pin_5
#define UART2_RX_GPIO_RCC           RCC_APB2Periph_GPIOD
#define UART2_RX_GPIO_PORT          GPIOD
#define UART2_RX_GPIO_PIN           GPIO_Pin_6
#define UART2_GPIO_PIN_REMAP        1
#else
#error 宏 UART2_PIN_SELECT 的值只能为 1 或 2
#endif


//#define UART3_EN                    1
//#define UART3_IT_RX_EN              1
//#define UART3_IT_TX_EN              1
//#define UART3_IT_IDLE_EN            1
#define UART3_PIN_SELECT            1

#define UART3_DEV                   USART2
#define UART3_DEV_DATA_LENGTH       USART_WordLength_8b
#define UART3_DEV_PARITY            USART_Parity_No
#define UART3_DEV_STOPBITS          USART_StopBits_1
#define UART3_DEV_BAUD_RATE         115200

#define UART3_ISR_MAINPRIO          1
#define UART3_ISR_SUBPRIO           2

#if (UART3_PIN_SELECT == 1)
#define UART3_TX_GPIO_RCC           RCC_APB2Periph_GPIOB
#define UART3_TX_GPIO_PORT          GPIOB
#define UART3_TX_GPIO_PIN           GPIO_Pin_10
#define UART3_RX_GPIO_RCC           RCC_APB2Periph_GPIOB
#define UART3_RX_GPIO_PORT          GPIOB
#define UART3_RX_GPIO_PIN           GPIO_Pin_11
#define UART3_GPIO_PIN_REMAP        0
#elif (UART3_PIN_SELECT == 2)
#define UART3_TX_GPIO_RCC           RCC_APB2Periph_GPIOC
#define UART3_TX_GPIO_PORT          GPIOC
#define UART3_TX_GPIO_PIN           GPIO_Pin_10
#define UART3_RX_GPIO_RCC           RCC_APB2Periph_GPIOC
#define UART3_RX_GPIO_PORT          GPIOC
#define UART3_RX_GPIO_PIN           GPIO_Pin_11
#define UART3_GPIO_PIN_REMAP        1
#elif (UART3_PIN_SELECT == 3)
#define UART3_TX_GPIO_RCC           RCC_APB2Periph_GPIOD
#define UART3_TX_GPIO_PORT          GPIOD
#define UART3_TX_GPIO_PIN           GPIO_Pin_8
#define UART3_RX_GPIO_RCC           RCC_APB2Periph_GPIOD
#define UART3_RX_GPIO_PORT          GPIOD
#define UART3_RX_GPIO_PIN           GPIO_Pin_9
#define UART3_GPIO_PIN_REMAP        2
#else
#error 宏 UART3_PIN_SELECT 的值只能为 1, 2 或 3
#endif



/** defgroup UART_Exported_MacroDefines
* @}
*/


/** @defgroup UART_Exported_Types uart exported types
* @brief 全局数据类型声明
* @{
*/

typedef void (*uart_isr_t)(USART_TypeDef *, uint16_t);
typedef enum
{
    UART_ISR_RX = 0,
    UART_ISR_TX,
    UART_ISR_TC,
    UART_ISR_IDLE,
    UART_ISR_ERROR,

    UART_ISR_TYPE_NUM
} UartIsrType_t;


/** defgroup UART_Exported_Types
* @}
*/






/*********************************************************************
                全局变量声明
*********************************************************************/







/*********************************************************************
                全局函数声明
*********************************************************************/

void Uart_init(void);
void Uart_isr_register(USART_TypeDef *UARTx, uint8_t type, uart_isr_t fn);

#ifdef UART1_EN
#define Uart1_send_str(str) Uart1_send((uint8_t *)str, strlen(str))
extern void Uart1_send(uint8_t *buf, uint16_t len);
extern void Uart1_send_byte(uint16_t c);
extern void Uart1_recv_byte(uint16_t c);
#endif

#ifdef UART2_EN
#define Uart2_send_str(str) Uart1_send((uint8_t *)str, strlen(str))
extern void Uart2_send(uint8_t *buf, uint16_t len);
extern void Uart2_send_byte(uint16_t c);
extern void Uart2_recv_byte(uint16_t c);
#endif

#ifdef UART3_EN
#define Uart3_send_str(str) Uart1_send((uint8_t *)str, strlen(str))
extern void Uart3_send(uint8_t *buf, uint16_t len);
extern void Uart3_send_byte(uint16_t c);
extern void Uart3_recv_byte(uint16_t c);
#endif

void Uart_set_stdout(USART_TypeDef *USARTx);
void Uart_putc(uint16_t c);
uint16_t Uart_getc(void);





/** addtogroup UART
* @}
*/

#ifdef __cplusplus
}
#endif
#endif // __UART_H__


/***  Copyright (C) 2022 wy1107412334@163.com All rights reserved ***/
/****************************** END OF FILE *************************/

